package cn.cjlmonster.demo02.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ScrollView;

/**
 * cn.cjlmonster.demo02.widget
 *
 * @author cjl
 * @project Demo02
 * @date 2022/4/13 10:09 下午
 */
public class MyScrollView extends ScrollView {

    private static final String TAG = "滑动监听测试";

    public MyScrollView(Context context) {
        super(context);
    }

    public MyScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private View dependency;

    public void setDependency(View dependency) {
        this.dependency = dependency;
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        int margin = dependency.getHeight();
        int scrollY = getScrollY();
        Log.i(TAG, "onTouchEvent: scrollY = " + getScrollY() + ", y = " + ev.getY());
        if (scrollY >= margin) {
            return super.onTouchEvent(ev);
        } else {
            int bottom = margin - scrollY;
            if (ev.getY() < bottom) return false;
        }

        return super.onTouchEvent(ev);
    }
}
